package com.roads.project.service.impl;

import java.util.List;

import com.roads.common.constant.UserConstants;
import com.roads.common.exception.ServiceException;
import com.roads.common.utils.DateUtils;
import com.roads.common.utils.StringUtils;
import com.roads.project.domain.ProProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.roads.project.mapper.ProCompanyMapper;
import com.roads.project.domain.ProCompany;
import com.roads.project.service.IProCompanyService;

/**
 * 单位（公司）Service业务层处理
 * 
 * @author nhp
 * @date 2021-11-16
 */
@Service
public class ProCompanyServiceImpl implements IProCompanyService {

    @Autowired
    private ProCompanyMapper proCompanyMapper;

    /**
     * 查询单位（公司）
     * 
     * @param companyId 单位（公司）主键
     * @return 单位（公司）
     */
    @Override
    public ProCompany selectProCompanyByCompanyId(Integer companyId) {
        return proCompanyMapper.selectProCompanyByCompanyId(companyId);
    }

    /**
     * 查询单位（公司）列表
     * 
     * @param proCompany 单位（公司）
     * @return 单位（公司）
     */
    @Override
    public List<ProCompany> selectProCompanyList(ProCompany proCompany) {
        return proCompanyMapper.selectProCompanyList(proCompany);
    }

    @Override
    public String checkCompanyNameUnique(ProCompany proCompany) {
        Integer companyId = StringUtils.isNull(proCompany.getCompanyId()) ? -1 : proCompany.getCompanyId();
        ProCompany search = proCompanyMapper.checkCompanyNameUnique(proCompany.getCompanyName());
        if (StringUtils.isNotNull(search) && !search.getCompanyId().equals(companyId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增单位（公司）
     * 
     * @param proCompany 单位（公司）
     * @return 结果
     */
    @Override
    public int insertProCompany(ProCompany proCompany) {
        proCompany.setCreateTime(DateUtils.getNowDate());
        return proCompanyMapper.insertProCompany(proCompany);
    }

    /**
     * 修改单位（公司）
     * 
     * @param proCompany 单位（公司）
     * @return 结果
     */
    @Override
    public int updateProCompany(ProCompany proCompany) {
        proCompany.setUpdateTime(DateUtils.getNowDate());
        return proCompanyMapper.updateProCompany(proCompany);
    }

    /**
     * 批量删除单位（公司）
     * 
     * @param companyIds 需要删除的单位（公司）主键
     * @return 结果
     */
    @Override
    public int deleteProCompanyByCompanyIds(Integer[] companyIds) {
        for (Integer companyId : companyIds) {
            ProCompany company = proCompanyMapper.selectProCompanyByCompanyId(companyId);
            if (proCompanyMapper.countInstitutionByCompanyId(companyId) > 0) {
                throw new ServiceException(String.format("%1$s已分配机构,不能删除", company.getCompanyName()));
            }
        }
        return proCompanyMapper.deleteProCompanyByCompanyIds(companyIds);
    }

    /**
     * 删除单位（公司）信息
     * 
     * @param companyId 单位（公司）主键
     * @return 结果
     */
    @Override
    public int deleteProCompanyByCompanyId(Integer companyId) {
        return proCompanyMapper.deleteProCompanyByCompanyId(companyId);
    }
}
